const mixin = require('../../mixins');
// const {mfutil} = getApp()
Component({
    behaviors: [mixin],
    data: {
        isFold: false, // 是否默认折叠
        speed: 0.5, // 折叠过渡时间
        panelObj: {},
		titleBarHeight: 0
    },
    properties: {
      mf_data: Object,
    },
    attached() {
        this.flodLock = true;
        this.initData();
    },
    detached() { },
    methods: {
        // 初始化数据
        initData() {
            const { mf_data } = this.properties;
            let obj = mf_data.children && mf_data.children.reduce((pre, cur) => {
                if (cur.className == '折叠头') {
                    pre['foldTitle'] = cur;
                    pre['foldTitleContent'] = cur.children.reduce((p, c) => {
						if(c.type =='hotSpace') {
							 p['href'] = c
							 p['children'].push(c);
						}else if(c.type =='text') {
							p['text'] = c
						}else {
						   p['children'].push(c);
						}
                        return p;
                    }, { href: '', children: [],text: '', isShow: false })
                } else if (cur.className == '折叠容器') {
                    pre['flodBox'] = cur
                }
                return pre;
            }, {})

				let systemInfo = wx.getSystemInfoSync();
					let pixelRatio1 = 750 /systemInfo.windowWidth;
				// this.statusBarHeight = systemInfo.statusBarHeight;
				let rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null; //胶囊按钮位置信息
				// wx.getMenuButtonBoundingClientRect();
				let navBarHeight = (function() {
				  //导航栏高度
				  let gap = rect.top - systemInfo.statusBarHeight; //动态计算每台手机状态栏到胶囊按钮间距
				  return 2 * gap + rect.height;
				})();
            this.setData({
                isFold: mf_data.collapsible,//是否默认折叠
                // speed: mf_data.speed || 0.1, //折叠过渡时间
                panelObj: obj, // 处理后的折叠面板对象
				titleBarHeight: navBarHeight,
				top: navBarHeight + 112/pixelRatio1 +systemInfo.statusBarHeight,
				top2: navBarHeight + 200/pixelRatio1 +systemInfo.statusBarHeight
            })
			if(obj.flodBox.trackingParams){
				this.triggerEvent('triggerComponent', {
					data: {
						trackData: obj.flodBox.trackingParams,
					}
				}, {
					bubbles: true,
					composed: true
				});
			}
        },

		updataData(d=null,goodsList) {
			if(!d) return
		    const { mf_data } = this.properties;
		    let obj = mf_data.children && mf_data.children.reduce((pre, cur) => {
		        if (cur.className == '折叠头') {
		            pre['foldTitle'] = cur;
		            pre['foldTitleContent'] = cur.children.reduce((p, c) => {
						p['isShow'] =true
						if(c.type =='hotSpace') {
							if(d.includes('件商品')) {
								c.href='pages/customer/recordList'
							}else {
								if(goodsList.length) {
									c.href= `pages/item/item?id=${goodsList[0].itemId}&valueId=${goodsList[0].valueId}`
								}
							}
							p['children'].push(c);
							p['href'] = c
		                }else if(c.type =='text') {
							c.text = d
							p['text'] = c
						}else {
						   p['children'].push(c);
						}
		                return p;
		            }, { href: '', children: [],text: '', isShow: false })
		        } else if (cur.className == '折叠容器') {
		            pre['flodBox'] = cur
		        }
		        return pre;
		    }, {})

				let systemInfo = wx.getSystemInfoSync();
					let pixelRatio1 = 750 /systemInfo.windowWidth;
				// this.statusBarHeight = systemInfo.statusBarHeight;
				let rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null; //胶囊按钮位置信息
				// wx.getMenuButtonBoundingClientRect();
				let navBarHeight = (function() {
				  //导航栏高度
				  let gap = rect.top - systemInfo.statusBarHeight; //动态计算每台手机状态栏到胶囊按钮间距
				  return 2 * gap + rect.height;
				})();
		    this.setData({
		        // isFold: mf_data.collapsible,//是否默认折叠
		        // speed: mf_data.speed || 0.1, //折叠过渡时间
		        panelObj: obj, // 处理后的折叠面板对象
				// titleBarHeight: navBarHeight,
				// top: navBarHeight + 112/pixelRatio1 +systemInfo.statusBarHeight
		    })

		},

		select(isFold) {
			this.setData({
			    isFold: isFold
			});
			return null
		}
    },
});
